﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_HelpType_Manager : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.RegisterJS();

        if (!IsPostBack)
        {
            Utility.InitUltraWebGrid.InitDisplayLayOut(this.UWGGoodType);

            BindData(0);
            LoadInitTree();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页       

        base.OnLoad(e);
    }

    #endregion

    protected void TVGoodType_SelectedNodeChanged(object sender, EventArgs e)
    {
        int ID = Shove._Convert.StrToInt(TVGoodType.SelectedNode.Value.ToString(),0);                   //商品类别编号

        BindData(ID);                                                           //重新梆定商品类别
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("HelpType_Edit.aspx?Action=Add");
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            Response.Redirect("HelpType_Edit.aspx?Action=Edit&ID=" + ID);
        }
    }

    protected void btnStop_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            string TypeStatus = UWGGoodType.DisplayLayout.ActiveRow.Cells.FromKey("TypeStatus").Value.ToString();
            if (TypeStatus.Equals("停用"))
            {
                Shove._Web.JavaScript.Alert(this.Page, "该类别已经停用！");
            }
            else
            {
                DAL.Tables.tbl_Site_HelpType table = new DAL.Tables.tbl_Site_HelpType();
                table.Status.Value = 0;
                table.Update("[ID]=" + Shove._Convert.StrToInt(Hidden1.Value,0));

                Hidden1.Value = "";
                this.BindData(0);
            }
        }
    }

    protected void btnResume_Click(object sender, EventArgs e)
    {
        string ID = Hidden1.Value;

        if (!ID.Equals(""))
        {
            string TypeStatus = UWGGoodType.DisplayLayout.ActiveRow.Cells.FromKey("TypeStatus").Value.ToString();
            if (TypeStatus.Equals("正常"))
            {
                Shove._Web.JavaScript.Alert(this.Page, "该类别已外于正常状态！");
            }
            else
            {
                DAL.Tables.tbl_Site_HelpType table = new DAL.Tables.tbl_Site_HelpType();
                table.Status.Value = 1;
                table.Update("[ID]=" + ID);

                Hidden1.Value = "";
                this.BindData(0);
            }
        }
    }

    protected void UWGGoodType_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        BindData(0);
    }

    /// <summary>
    /// 加载客服中心类别信息
    /// </summary>
    private void BindData(int ID)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        string Fields = "ID,TypeName,";
        Fields += "(case when Status ='1' then '正常' else '停用' end )as TypeStatus,ParentID,Introduce";
        string Condition = "(ShopID=" + _Site.ID + ")";
        string Sort = "";

        if (ID != 0)
        {
            Condition = "(ShopID=" + _Site.ID + ") and ID=" + ID;
        }

        dt = new DAL.Tables.tbl_Site_HelpType().Open(Fields, Condition, Sort);

        UWGGoodType.DataSource = dt;
        UWGGoodType.DataBind();
    }

    /// <summary>
    /// 加载商品类别信息到树中
    /// </summary>
    private void LoadInitTree()
    {
        string ID = "";

        string Fields = "ID,TypeName,";
        Fields += "(case when Status =1 then '正常' else '停用' end )as TypeStatus,ParentID,Introduce";
        string Condition = "(ShopID=" + _Site.ID + ")";
        string Sort = "";

        if (!ID.Equals(""))
        {
            Condition = "(ShopID=" + _Site.ID + ")  and ID=" + ID;
        }

        dt = new DAL.Tables.tbl_Site_HelpType().Open(Fields, Condition, Sort);

        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID=0";

        //添加一根节点
        TreeNode node1 = new TreeNode();
        node1.Text = "所有类别";
        node1.Value = "0";
        TVGoodType.Nodes.Add(node1);

        foreach (DataRowView drv in dv)
        {
            TreeNode node = new TreeNode();
            node.Text = drv["TypeName"].ToString();
            node.Value = drv["ID"].ToString();
            node.Expanded = false;
            node1.ChildNodes.Add(node);
            AddReplies(dt, node);
        }
    }

    /// <summary>
    /// 递归绑定商品类别到树中 
    /// </summary>
    private void AddReplies(DataTable dt, TreeNode node)
    {
        DataView dv = new DataView(dt);
        dv.RowFilter = "ParentID=" + node.Value;

        foreach (DataRowView row in dv)
        {
            TreeNode replyNode = new TreeNode();
            replyNode.Text = row["TypeName"].ToString();
            replyNode.Value = row["ID"].ToString();
            replyNode.Expanded = false;
            node.ChildNodes.Add(replyNode);
            AddReplies(dt, replyNode);
        }
    }

    /// <summary>
    /// 初始化JS 
    /// </summary>
    private void RegisterJS()
    {
        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.UWGGoodType, "ig_webgrid_an");          //注册 Infragistics js和控件
    }

}

